/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETU_CreateIt.cc
  \date			september 2011
  \author		TNick

  \brief		Contains the implementation for ETU_CreateIt class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<main.h>

#include	<sheet/ETSheetView.h>
#include	<sheet/ETSheet.h>
#include	<sheet/ETI_CplxTxt.h>

#include	<sheet/ETUndoElem.h>

#include	"ETU_CreateIt.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
ETU_CreateIt::ETU_CreateIt	(
	ETUndoElem::Type el_ty, QGraphicsItem * it_to_handle )
	: ETUndoElem (el_ty)
{
	Q_ASSERT_X((
				   (el_ty == ETUndoElem::CREATE_ITEM) |
				   (el_ty == ETUndoElem::DELETE_ITEM)),
			   "ETU_CreateIt::ETU_CreateIt",
			   "Expected create / delete item type, ggot something else");
	if (el_ty == ETUndoElem::CREATE_ITEM)
		{
			setUnMirrored();
			setDescription(
						QString(
							"Created item at (%1, %2)"
							)
						.arg(it_to_handle->x())
						.arg(it_to_handle->y()));
		}
	else
		{
			setMirrored();
			setDescription(
						QString(
							"Deleted item at (%1, %2)"
							)
						.arg(it_to_handle->x())
						.arg(it_to_handle->y()));
		}
	assoc_it = it_to_handle;
	sheet = static_cast<ETSheet *>(it_to_handle->scene());

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETU_CreateIt::~ETU_CreateIt	( void )
{
	if (assoc_it != NULL)
		{
			/* the instance shall delete the object it handles if it has: */
			if (fromDelete())
				{
					/* - ETUndoElem::DELETE_ITEM type and expires in undo chain */
					if (isUndoReady())
						{
							delete assoc_it;
						}
				}
			else
				{
					/* - ETUndoElem::CREATE_ITEM type and expires in redo chain */
					if (isRedoReady())
						{
							delete assoc_it;
						}
				}
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETU_CreateIt::performUndo	( void )
{
	if (!isUndoReady())
		{
			qDebug() << "ETU_CreateIt::performUndo for an item in redo state:"
					 << this;
			return;
		}

	if (fromDelete())
		{ /* undo means to insert the item in the place it was before */
			sheet->addItem(assoc_it);
		}
	else
		{ /* undo means to delete the item */
			sheet->removeItem(assoc_it);
		}


	setRedoReady();

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETU_CreateIt::performRedo	( void )
{
	if (!isRedoReady())
		{
			qDebug() << "ETU_CreateIt::performRedo for an item in undo state:"
					 << this;
			return;
		}

	if (fromDelete())
		{ /* redo means to delete the item */
			sheet->removeItem(assoc_it);
		}
	else
		{ /* redo means to insert the item in the place it was before */
			sheet->addItem(assoc_it);
		}


	setUndoReady();
}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
